#!/usr/bin/env python
# coding: utf-8

# In[1]:


classmates = ['Michael', 'Bob', 'Tracy']
classmates


# In[2]:


len(classmates)


# In[3]:


print(classmates[-1])


# In[8]:


classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[0])


# In[5]:


print(classmates[-2])


# In[9]:


classmates = ['Michael', 'Bob', 'Tracy']
classmates.append('Adam')
classmates


# In[15]:


classmates.insert(2,'eve')
classmates
classmates.pop(1)
print(classmates)


# In[17]:


classmates = ['Michael', 'Bob', 'Tracy']
classmates.append('Adam')
classmates.insert(2,'Eva')
print(classmates)


# In[18]:


classmates[2] = 'eve'
print(classmates)


# In[20]:


classmates.insert(1, 100)
print(classmates)


# In[21]:


newstudents = ['Tom', 'Jerry']
classmates.append(newstudents)
print(classmates)


# In[22]:


classmates.pop(0)
print(classmates)


# In[23]:


classmates[6][1] #第一个方括号是第7个，第二个方括号是列表的第二个


# In[24]:


#tuple元组


# In[25]:


classmates = ('Michael', 'Bob', 'Tracy')
classmates[1] = 'tom'
print(classmates)


# In[27]:


classmates = ('Mike', 'Eva', 'Jupyter')
t = (1,) #带有一个元素的元组
print(t)
print(classmates[0])


# In[30]:


classmates = ('Mike', 'Eva', 'Jupyter', 'Mike')
print(classmates.count('Mike'))
print(classmates.count('999'))
print(classmates.index('Eva')) #下标


# In[41]:


#构造命名元组
from collections import namedtuple
student_info = namedtuple('stud_info', 'name, id, gender, age, score')
#赋值
studinf = student_info(name = 'xiaocheng', id = '0001', gender = 'male', age = 22, score = 100)
#输出值，某一个元素是什么
print(studinf[0], studinf[1], studinf[2], studinf[3], studinf[4])# 太复杂

print(studinf.name, studinf.id, studinf.gender, studinf.age, studinf.score) #可看到具体名字，方便

print("name:{},id:{}, gender:{}, age:{}, score:{}".format(studinf.name, studinf.id, studinf.gender, studinf.age, studinf.score))


# In[37]:


#构造命名元组
from collections import namedtuple
student_info = namedtuple('stud_info', 'name, id, gender, age, score')
value = ['xiaochen', '0001', 'male', 22, 100]
studinf = student_info._make(value)

newstud = studinf._replace(name = 'xiaowang', id = '0002', gender = 'male')
print(newstud.name, newstud.id, newstud.gender)


# In[40]:


from collections import namedtuple
import csv
student_info = namedtuple('stud_info', 'name, id, gender, age, score')
reader = csv.reader(open("E:\\a.csv","r"))
for r in reader:
    studinf = student_info._make(r)
    print(studinf)


# In[48]:


#字典，dict
dscore = {'Tom':99, 'Jerry':88, 'Mike':90}
print(dscore)
print(dscore['Jerry'])
dscore['Jerry'] = 100
print(dscore)


# In[58]:


print('Tom' in dscore)
print(dscore.get('Tom'))
print(dscore.get('Tommy'))
print(dscore)


# In[61]:


s = set(1,2,3)#多个参数不可，只能有一个参数，用list

s = set([1,2,3])
s


# In[62]:


s = set([1,1,2,2,2,2,3])
s


# In[65]:


s.add(4)
s.add(4)
print(s)
s.remove(4) # set中的删除元素，remove
s


# In[74]:


sdouyin = set(['xiaowang', 'xiaocheng', 'xiaoli'])
skuaishou = set(['xiaocheng', 'xiaozhu', 'xiaozhao'])
sshortvideuser = sdouyin & skuaishou
#sdouyin.intersection(skuaishou)
s = sdouyin | skuaishou
sdif = sdouyin - skuaishou
sdif1 = skuaishou - sdouyin 

sdifs = sdouyin ^ skuaishou
print(sdifs)
print(sdif)
print(sdif1)
print(s)
print(sshortvideuser)


# In[75]:


#下午：控制结构与函数


# In[77]:


a = 5 
if a < 0:
    a = -a
print(a)


# In[82]:


height = int(input('please enter your height:'))
if height <= 175:
    print('CONGRATULATIONS')
else:
    print('sorry')


# In[83]:


height = float(input('please enter your height: '))
weight = float(input('please enter your weight: '))
BMI = weight / height**2
result = ''
if BMI < 18.5:
    result = '过轻'
elif 18.5 <= BMI < 25:
    result = '正常'
elif 25 <= BMI < 28:
    result = '过重'
elif 28 <= BMI < 32:  
    result = '肥胖'
elif BMI > 32:
    result = '严重肥胖'
print(result)


# In[85]:


names = ['xiaowang', 'xiaochen', 'xiaozhao']
for every_name in names:
    print(every_name)


# In[88]:


l = [1,2,3,4,5]
sum = 0
for every_num in l:
    sum =sum + every_num #sum += every_num
print(sum)


# In[89]:


#求最大值
l = [1,2,3,4,5]
max = 0
for x in l:
    if x > max:
        max = x
print(max)
        


# In[90]:


sum = 0
for every_num in range(101):
    sum = sum + every_num
print(sum)
    


# In[91]:


sum = 0
for every_num in range(1,101,2):
     sum = sum + every_num
print(sum)


# In[92]:


sum = 0
for every_num in range(0,201,2):
     sum = sum + every_num
print(sum)


# In[97]:


name = input('please enter your name:')
strname = ''
for n in name:
    if 'a' <= n <= 'z':
        strname += n.upper()

    elif 'A' <= n <= 'Z':
        strname += n.lower()
print(strname)


# In[100]:


names = ['xiaowang', 'xiaochen', 'xiaozhao']
for every_name in names:
    print(every_name)
    
i = 0
while i < len(names):
    print(names[i])
    if names[i] == 'xiaochen':
        break;
    i = i + 1


# In[102]:


names = ['xiaowang', 'xiaochen', 'xiaozhao']
for every_name in names:
    print('Hello', every_name)


# In[106]:


#自定义函数

name = input('please enter your name:')

def convert(source):
    strname = ''
    for n in source:
        if 'a' <= n <= 'z':
            strname += n.upper()
        elif 'A' <= n <= 'Z':
            strname += n.lower()
    return strname


#函数的调用
name = input('please enter your name:')
print(convert(name))





# In[111]:




def adder(source1,source2):
    result = source1 + source2
    return result

print(adder(1,2))


   


# In[112]:


def power(x, n = 2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
power(2,3)
power(2)


# In[113]:


def power(x =3, n = 2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
power(2,3)
power(n = 5)


# In[117]:


#允许计算两个乘积，改造，变成可接受一个或多个
def product(x, *y):
    proresult = 1
    for num in y:
        proresult = proresult * num
        
    return x * proresult
print(product(1,2,3,4,5))


# In[ ]:




